//
//  YZEFavoriteViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/3/29.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "UIImage+Layout.h"
#import "YZEFavoriteViewController.h"
#import "YZEMyFavoriteTableViewCell.h"
#import "YZECommunityTableViewCell2.h"
#import "collectionModel.h"
#import <UIImageView+WebCache.h>
#import "YZEHomeClickViewController.h"
#import "TopicDetailViewController.h"
#import "KnowledgeDetailViewController.h"

#define DeleteURL @"/api/lore-collections/"
#define URL_Getfavorite @"/api/lore-collections/mine"

@interface YZEFavoriteViewController ()
//@property(nonatomic,strong)NSMutableArray *allData;
@property (nonatomic,assign)CGFloat cellHeight;
@property (nonatomic,strong)NSMutableArray *dataArr;
@end

@implementation YZEFavoriteViewController
-(NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"我的收藏";

    [self.tableView registerNib:[UINib nibWithNibName:@"YZECommunityTableViewCell2" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"favor_topic_cell"];
    
    [[YZENetwork sharedInstace]getRequestWithUrl:[BASE stringByAppendingString:URL_Getfavorite] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        self.dataArr = [collectionModel mj_objectArrayWithKeyValuesArrayAdd:responseObject];
        [self.tableView reloadData];
    }];
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editTableView:)];
}
-(void)editTableView:(UIBarButtonItem*)sender
{
    [sender setTitle:self.tableView.isEditing?@"编辑":@"完成" ];
    [self.tableView setEditing:!self.tableView.editing animated:YES];//带动画
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellHeight;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    collectionModel *model=self.dataArr[indexPath.row];
    if (model.articleId && model.articleId>0) { //新闻文章
        YZEMyFavoriteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"favor_view_cell"];
        if (!cell) {
            cell = [[YZEMyFavoriteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"favor_view_cell"];
        }
        cell.m_label_title.text = model.articleDTO.name;
        cell.m_label_writedate.text = model.articleDTO.createdDate;
        if (model.articleDTO.uri) {
            [cell.m_imgview_article_head_img sd_setImageWithURL:[NSURL URLWithString:model.articleDTO.uri] placeholderImage:[UIImage imageNamed:@"my"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                UIImage *new_image = [image scaleToSize:CGSizeMake(86, 83)];
                [cell.m_imgview_article_head_img setImage:new_image];
            }];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.cellHeight = 100;
        return cell;
    }else if ([model.topic.ID longLongValue]>0) { //话题
        YZECommunityTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"favor_topic_cell"];
        if (!cell) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"favor_topic_cell" forIndexPath:indexPath];
        }
        [cell showData:model.topic superWidth:self.tableView.frame.size.width];
        self.cellHeight = [cell cellHeight:model.topic superWidth:self.tableView.frame.size.width];
        return cell;
    }else{ //移植知识
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        }
        cell.textLabel.text = model.lore.name;
        cell.detailTextLabel.text = model.lore.createdDate;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.cellHeight = 60;
        return cell;
    }
}

//点击了删除如何处理
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    collectionModel *model=self.dataArr[indexPath.row];
    
    [[YZENetwork sharedInstace]deleteRequestWithUrl:[NSString stringWithFormat:@"%@%@%ld",BASE,DeleteURL,model.id] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        [UIView showToastWindowWithMessage:@"收藏已删除"];
        //删除表格中的某行
        [self.dataArr removeObjectAtIndex:indexPath.row]; //需要先把元素从数组中删除
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; //带动画删除
    }];
}

//点击跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    collectionModel *model=self.dataArr[indexPath.row];
    if (model.articleId && model.articleId>0) { //新闻文章
        YZEHomeClickViewController *yzecVC = [YZEHomeClickViewController new];
        yzecVC.title = model.articleDTO.subtitle;
        yzecVC.isPresented = YES;
        [yzecVC getWebID:[NSString stringWithFormat:@"%ld", model.articleId]];
        UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:yzecVC];
        [self presentViewController:navi animated:YES completion:nil];
    }else if ([model.topic.ID longLongValue]>0) { //话题的跳转
        TopicDetailViewController *detail=[TopicDetailViewController new];
        UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:detail];
        detail.topicDetail = model.topic;
        [self presentViewController:navi animated:YES completion:nil];
    }else{ //移植知识的跳转
        KnowledgeDetailViewController *detail=[KnowledgeDetailViewController new];
        UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:detail];
        collectionModel *model=self.dataArr[indexPath.row];
        [detail getWebID:[NSString stringWithFormat:@"%ld", model.lore.id]];
        [self presentViewController:navi animated:YES completion:nil];
        
    }
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

@end
